﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Collections;

namespace CitrixPVSWrapper
{
    
    [System.Management.Automation.Cmdlet("Run", "PvsCopyPasteDiskLocator", SupportsShouldProcess = true)]
    public class Run_PvsCopyPasteDiskLocator : System.Management.Automation.PSCmdlet
    {
        [Parameter(Mandatory = false)]
        public PvsDiskLocator SourceDiskLocator;

        [Parameter(Mandatory = false)]
        public String SourceDiskLocatorId;

        [Parameter(Mandatory = false)]
        public PvsDiskLocator DestinationDiskLocator;

        [Parameter(Mandatory = false)]
        public String DestinationDiskLocatorId;

        [Parameter(Mandatory = false)]
        public DiskLocatorPropertySet[] Properties;

        protected override void BeginProcessing()
        {
            // Test for mutually exclusive parameters
            Validate.MutuallyExclusiveParameters(this.MyInvocation.BoundParameters, new string[] { "SourceDiskLocator", "SourceDiskLocatorId"},true);
            Validate.MutuallyExclusiveParameters(this.MyInvocation.BoundParameters, new string[] { "DestinationDiskLocator", "DestinationDiskLocatorId"}, true);

            // Test that the supplied Source Disk Locator exists
            if (MyInvocation.BoundParameters.ContainsKey("SourceDiskLocator")) 
            {
                if (Get_PvsDiskLocator_Internal.Get_DiskLocatorById(this.SourceDiskLocator.DiskLocatorId).Count == 1)
                {
                    this.SourceDiskLocatorId = this.SourceDiskLocator.DiskLocatorId;
                }
                else
                {
                    throw new ParameterBindingException("The specified Source Disk Locator could not be found");
                }
            }
            else if (MyInvocation.BoundParameters.ContainsKey("SourceDiskLocatorId")) 
            {
                if (Get_PvsDiskLocator_Internal.Get_DiskLocatorById(this.SourceDiskLocatorId).Count == 1)
                {
                    this.SourceDiskLocator = Get_PvsDiskLocator_Internal.Get_DiskLocatorById(this.SourceDiskLocatorId).First();
                }
                else
                {
                    throw new ParameterBindingException("The specified Source Disk Locator Id could not be found");
                }
            }
            
            // Test that the supplied Destination Disk Locator exists
            if (MyInvocation.BoundParameters.ContainsKey("DestinationDiskLocator"))
            {
                if (Get_PvsDiskLocator_Internal.Get_DiskLocatorById(this.DestinationDiskLocator.DiskLocatorId).Count == 1)
                {
                    this.DestinationDiskLocatorId = this.DestinationDiskLocator.DiskLocatorId;
                }
                else
                {
                    throw new ParameterBindingException("The specified Destination Disk Locator could not be found");
                }
            }
            else if (MyInvocation.BoundParameters.ContainsKey("DestinationDiskLocatorId"))
            {
                if (Get_PvsDiskLocator_Internal.Get_DiskLocatorById(this.DestinationDiskLocatorId).Count == 1)
                {
                    this.DestinationDiskLocator = Get_PvsDiskLocator_Internal.Get_DiskLocatorById(this.DestinationDiskLocatorId).First();
                }
                else
                {
                    throw new ParameterBindingException("The specified Destination Disk Locator Id could not be found");
                }
            }
        }

        protected override void ProcessRecord()
        {
            // Verbose and Debug Settings
            Boolean Verbose = this.MyInvocation.BoundParameters.ContainsKey("Verbose");
            WriteVerbose("Verbose Flag: " + Verbose);
            Boolean Debug = this.MyInvocation.BoundParameters.ContainsKey("Debug");
            WriteDebug("Debug Flag: " + Debug);

            // Collections to hold the queries and the results
            string TargetObject = "CopyPasteDiskLocator";

            //Build the query object
            QueryDefinition RunQuery = new QueryDefinition(QueryActionType.Run, TargetObject);

            RunQuery.Parameters.Add("diskLocatorIdFrom=\"" + this.SourceDiskLocatorId + "\"");
            RunQuery.Parameters.Add("diskLocatorId=\"" + this.DestinationDiskLocatorId + "\"");
            if (this.MyInvocation.BoundParameters.ContainsKey("Properties")) { RunQuery.Parameters.Add("properties=" + String.Join(",", this.Properties.ToString())) ;}

            string ShouldProcessMessage = String.Format("Copy properties from disk locator {0} to disk locator {1}", this.SourceDiskLocatorId, this.DestinationDiskLocatorId);

            // Perform the actual Run and return any errors
            if (ShouldProcess(ShouldProcessMessage))
            {
                MCliReturnObject Return = PvsHelpers.ExecuteMcliAction(RunQuery, Verbose, Debug);
                if (Return.ReturnState == ReturnState.Error) { foreach (ErrorRecord Error in Return.Errors) { WriteError(Error); } }
            }

        }
    }

    // Property set to be used for specifying the properties to be copied between devices. 
    public enum DiskLocatorPropertySet
    {
        Description =1,
        Class = 2,
        Type = 3,
        DiskMode = 4,
        AutoUpdate = 5,
        HA = 6,
        ActiveDirectory = 7,
        Printer = 8,
        Version = 10,
        Date = 11,
        Author = 12, 
        Title = 13,
        Company = 14,
        InternalFilename = 15,
        OriginalFilename = 16,
        HardwareTarget = 17,
        MenuText = 18,
        Enabled = 19,
        Server =20,
        Store = 21
    }

}
